package com.jiuzhou.guanwang.jzcp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.activity.ExpDetailActivity;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ForecastFragment extends Fragment {
    Unbinder unbinder;
    private View rootView;

    public static ForecastFragment newInstance(String info) {
        Bundle args = new Bundle();
        ForecastFragment fragment = new ForecastFragment();
        args.putString("info", info);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.get("info").toString();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_forecast, null);
            initView();
        } else {
            // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，
            // 要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }

        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    private void initView() {
    }

    private void jumpToDetail(String s) {
        Intent intent = new Intent(getActivity(), ExpDetailActivity.class);
        intent.putExtra("userId", s);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_exp1, R.id.rl_exp2, R.id.rl_exp3, R.id.rl_exp4, R.id.rl_exp5, R.id.rl_exp6, R.id.rl_exp7, R.id.rl_exp8, R.id.rl_exp9, R.id.rl_exp10})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.rl_exp1:
                jumpToDetail("500");
                break;
            case R.id.rl_exp2:
                jumpToDetail("501");
                break;
            case R.id.rl_exp3:
                jumpToDetail("502");
                break;
            case R.id.rl_exp4:
                jumpToDetail("503");
                break;
            case R.id.rl_exp5:
                jumpToDetail("513");
                break;
            case R.id.rl_exp6:
                jumpToDetail("514");
                break;
            case R.id.rl_exp7:
                jumpToDetail("842");
                break;
            case R.id.rl_exp8:
                jumpToDetail("830");
                break;
            case R.id.rl_exp9:
                jumpToDetail("26674");
                break;
            case R.id.rl_exp10:
                jumpToDetail("564");
                break;
        }
    }
}
